package com.fenqiyong.www.action;

import android.content.Context;

import com.orhanobut.logger.Logger;
import com.tougc.bean.beans.MemberInfo14K;
import com.tougc.bean.beans.Successful14K;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import ren.nearby.http.request.BaseObserver;
import ren.nearby.http.request.HttpApi;
import ren.nearby.http.request.RxResultHelper;
import ren.nearby.http.request.SchedulersCompat;
import ren.nearby.http.utils.util.RxDisposble;
import ren.nearby.http.view.progress.DialogRequest;

/**
 * Created by Administrator on 2018/5/18 0018.
 */

public class ALoginActPresent implements ALoginActContract.Action {
    ALoginActContract.View mView;  // 需要抽象出来
    @Inject
    HttpApi mApi;
    @Inject
    Context context;

    @Inject
    ALoginActPresent() {
    }

    @Override
    public void takeView(ALoginActContract.View view) {
        mView = view;
    }

    @Override
    public void dropView() {
        mView = null;
        RxDisposble.getInstance().unSubscribe();
    }


    @Override
    public void smsSend(String mobile) {
        Observable ob = mApi.smsSend(mobile);
        BaseObserver observer = new BaseObserver<Successful14K>(mView.getActivity()) {


            @Override
            public void onNext(Successful14K k14) {
                mView.startCode();

            }
        };
        ob.compose(SchedulersCompat.IO_TRANSFORMER)//处理线程
                .compose(RxResultHelper.handleResult14K())
                .subscribe(observer);
    }

    @Override
    public void login(String mobile, String captcha) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("mobile", mobile);
        map.put("captcha", captcha);
        Observable ob = mApi.login(map);
        BaseObserver observer = new BaseObserver<MemberInfo14K>(mView.getActivity()) {


            @Override
            public void onNext(MemberInfo14K k14) {
                Logger.e("登录成功");
                mView.startLock();

            }
        };
        ob.compose(SchedulersCompat.IO_TRANSFORMER)//处理线程
                .compose(RxResultHelper.handleResult14K())
                .compose(SchedulersCompat.applyProgress(mView.getActivity(), DialogRequest.TAG_REQUEST))
                .subscribe(observer);
    }

    @Override
    public void smsCheck(String mobile, String captcha) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("mobile", mobile);
        map.put("captcha", captcha);
        Observable ob = mApi.login(map);
        BaseObserver observer = new BaseObserver<MemberInfo14K>(mView.getActivity()) {


            @Override
            public void onNext(MemberInfo14K k14) {
                Logger.e("登录成功");
                mView.startLock();

            }
        };
        ob.compose(SchedulersCompat.IO_TRANSFORMER)//处理线程
                .compose(RxResultHelper.handleResult14K())
                .compose(SchedulersCompat.applyProgress(mView.getActivity(), DialogRequest.TAG_REQUEST))
                .subscribe(observer);
    }
}


